clear
* end user likely needs to change this path
**  Read the replication data
insheet using "data\repdata.csv"


******************************** Preprocessing ANALYSIS

** state dummies
encode state, gen(ST_ID)
* Pct T - Pct R
gen trump_pctdiff = (pct_trump - pct_romney)*100
* lagged DV
gen romney_pctdiff = (pct_romney - pct_mccain) * 100
** difference in difference (I don't use this, but same substantive results)
gen pct_diffdiff = trump_pctdiff - romney_pctdiff
* fop main explanatory vars
gen lodges_per_10k = foplodge / cvap_est20112015 * 10000
gen log_lodge_density = log(lodges_per_10k + 1)
egen rank_lodge_density = rank(log_lodge_density)

* generate robustness check independent variables
* no adjacency weights, no log
gen lodges_nocontig_per10k = county_lodges / cvap_est20112015 * 10000
* no adjacency weights, logged
gen log_lodges_nocontig_per10k = log(lodges_nocontig_per10k+1)

******************************** MAIN ANALYSIS

* Table 3
estimates clear
eststo: reg trump_pctdiff log_lodge_, r
eststo: areg trump_pctdiff log_lodge_ , r absorb(ST)
eststo: areg trump_pctdiff log_lodge_ blueco prot laborden assn whitenoco *_lq* whitenonh rucc pct_vet hisppct poverty2015 income2015 romney_pctdiff, rob absorb(ST)
eststo: areg trump_pctdiff log_lodge_ blueco prot laborden assn whitenoco *_lq* whitenonh rucc pct_vet hisppct poverty2015 income2015 romney_pctdiff  [aweight=cvap_est20112015], rob absorb(ST)

esttab, se(4) bic ar2 

*esttab using "C:\Users\michael\Documents\fop\psp_v\revisions\2ndRevisions\\table12.tex", se(4) bic ar2 label  booktabs ///
   *title(Regression table\label{tab1})
	
******************************** IMPACT ANALYSIS

** Get "counterfactual" election
areg trump_pctdiff log_lodge_ blueco prot laborden assn whitenoco *_lq* whitenonh rucc pct_vet hisppct poverty2015 income2015 romney_pctdiff  [aweight=cvap_est20112015], rob absorb(ST)
* .8898729 [.4468088, 1.332937]
* note that difference variables were multiplied by 100 earlier in code, hence the division here
gen counterfactual_vs = pct_trump - ( .8898729  /100)*log_lodge_
gen counterfactual_trumpvote = round(counterfactual_vs*total_2016)
egen cf_statetrumpvotetotal = sum(counterfactual_trumpvote), by(ST)
egen actual_totalvotetotal = sum(total_2016), by(ST)
egen actual_stateclinton = sum(dem_2016), by(ST)
egen actual_statetrump = sum(gop_2016), by(ST)
gen cf_trumpvoteshare = cf_statetrumpvotetotal/actual_totalvotetotal
gen actual_clintonvoteshare = actual_stateclinton/actual_totalvotetotal
gen actual_trumpvoteshare = actual_statetrump/actual_totalvotetotal

* summarize in table
** mean by state gets state results because every county-row in data contains columns representing the state's results
tabstat actual_clintonvoteshare actual_trumpvoteshare cf_trumpvoteshare, statistics( mean ) by(state)
	
******************************** ROBUSTNESS TESTS

*** not logged independent variable
eststo: reg trump_pctdiff lodges_per_10k, r
eststo: areg trump_pctdiff lodges_per_10k , r absorb(ST)
eststo: areg trump_pctdiff lodges_per_10k blueco prot laborden assn whitenoco *_lq* whitenonh rucc pct_vet hisppct poverty2015 income2015 romney_pctdiff, rob absorb(ST)
eststo: areg trump_pctdiff lodges_per_10k blueco prot laborden assn whitenoco *_lq* whitenonh rucc pct_vet hisppct poverty2015 income2015 romney_pctdiff  [aweight=cvap_est20112015], rob absorb(ST)

*** no adjacency weights independent variable
eststo: reg trump_pctdiff log_lodges_nocontig_per10k, r
eststo: areg trump_pctdiff log_lodges_nocontig_per10k , r absorb(ST)
eststo: areg trump_pctdiff log_lodges_nocontig_per10k blueco prot laborden assn whitenoco *_lq* whitenonh rucc pct_vet hisppct poverty2015 income2015 romney_pctdiff, rob absorb(ST)
eststo: areg trump_pctdiff log_lodges_nocontig_per10k blueco prot laborden assn whitenoco *_lq* whitenonh rucc pct_vet hisppct poverty2015 income2015 romney_pctdiff  [aweight=cvap_est20112015], rob absorb(ST)

*** not logged, no adjacency
est clear
eststo: reg trump_pctdiff lodges_nocontig_per10k, r
eststo: areg trump_pctdiff lodges_nocontig_per10k , r absorb(ST)
eststo: areg trump_pctdiff lodges_nocontig_per10k blueco prot laborden assn whitenoco *_lq* whitenonh rucc pct_vet hisppct poverty2015 income2015 romney_pctdiff, rob absorb(ST)
eststo: areg trump_pctdiff lodges_nocontig_per10k blueco prot laborden assn whitenoco *_lq* whitenonh rucc pct_vet hisppct poverty2015 income2015 romney_pctdiff  [aweight=cvap_est20112015], rob absorb(ST)

** using rank of lodge density
 areg trump_pctdiff rank_lodge_de blueco prot laborden assn whitenoco *_lq* whitenonh rucc pct_vet hisppct poverty2015 income2015 romney_pctdiff  [aweight=cvap_est20112015], rob absorb(ST)
